package web.myServlet;

import entity.Car.bo.CarBo;
import entity.Car.model.Car;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import web.beans.CarViewBean;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * Created by vanches on 13.1.15.
 */

public class CarServlet extends HttpServlet {
    public static final int SC_NOT_FOUND = 404;


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            try {
                String IsLogout = request.getParameter("id");


                HttpSession session = request.getSession();

                String ResultPage = null; //Страница перехода при успешном
                CarViewBean carViewBean = (CarViewBean) session.getAttribute("carViewBean");
                ServletContext servletContext = getServletContext();
                ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
                CarBo carBo = (CarBo)ctx.getBean("carBo");
                Car car=null;
                if (carViewBean == null)
                    carViewBean = new CarViewBean();



                try {
                    car = carBo.getCarId(new Integer(IsLogout));
                }
                catch (Exception e){
                    response.sendError(HttpServletResponse.SC_NOT_FOUND);
                }

                    ResultPage = "/advertisement/review.xhtml";
                    carViewBean.setCar(car);
                    session.setAttribute("carViewBean", carViewBean);


                ServletContext SC = getServletContext();
                RequestDispatcher Disp = SC.getRequestDispatcher(ResultPage);
                Disp.forward(request, response);
            } catch (Throwable theException) {
                theException.printStackTrace();
            }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }


}
